//
//  CaiWuManageViewController.m
//  LazyCat
//
//  Created by jike on 15/12/30.
//  Copyright (c) 2015年 zhanshu. All rights reserved.
//

#import "CaiWuManageViewController.h"
#import "CaiWuTableViewCell.h"
#import "OtherTableCell.h"
#import "AFNetworkHandler.h"
#import "html5MYViewController.h"

@interface CaiWuManageViewController ()<UITableViewDataSource,UITableViewDelegate>

{
    UITableView *_tableView;
    UIView *_headView; // 头部view
    UILabel *_moneyLable; // 今日收益
 
    NSString *cashbalance;// 可提现余额
    NSString *invitescore;// 分享积分
    
}
@end

@implementation CaiWuManageViewController

-(void)viewWillAppear:(BOOL)animated
{
    if (kNetworkNotReachability) { // 没网
         
        [[MessShowView share] showWithMess:@"当前网络不给力"];
    }
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    cashbalance = @"0.00";
    invitescore = @"0.00";
    
    [self createTable];
    [self getData];
    
}

-(void)getData
{
    NSString *url = [NSString stringWithFormat:ScoreInfotUrl,[AppData share].username];
    [AFNetworkHandler GETWithAFNByURL:url completion:^(id result) {
        
        NSDictionary *dic = result;
        if ([dic[@"r"] isEqualToString:@"T"]) {
            
          NSDictionary  *dataDic = dic[@"scoreinfo"];
            
             // 今日收益
             NSString *str1 = [NSString stringWithFormat:@"%@",dataDic[@"currenttodayincome"]];
            float flat1 = [str1 floatValue];
            _moneyLable.text = [NSString stringWithFormat:@"%.2f",flat1];
            // 可提现余额
           NSString *str2= [NSString stringWithFormat:@"%@",dataDic[@"cashbalance"]];
             float flat2 = [str2 floatValue];
            cashbalance = [NSString stringWithFormat:@"%.2f",flat2];
            
            // 分享积分
           NSString *str3= [NSString stringWithFormat:@"%@",dataDic[@"invitescore"]];
            float flat3 = [str3 floatValue];
            invitescore = [NSString stringWithFormat:@"%.2f",flat3];
            
        }
        [_tableView reloadData];
        
    }];
}


//修改状态栏的颜色
-(UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

-(void)createTable
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-60) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    _headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 150)];
    _headView.backgroundColor = MainColor;
    
    
    UIImageView *imv = [[UIImageView alloc] initWithFrame:CGRectMake(18, 30, 12, 21)];
    imv.image = [UIImage imageNamed:@"lALOAy2ooyYW_22_38"];
    [_headView addSubview:imv];
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
     cancelBtn.frame = CGRectMake(5, 20, 60, 40);
    [cancelBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [_headView addSubview:cancelBtn];
#warning 问号图片
    UIImageView *wenhao = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth - 30, 32, 20, 20)];
    wenhao.image = [UIImage imageNamed:@"wenhao.png"];
    [_headView addSubview:wenhao];
    wenhao.userInteractionEnabled = YES;
    UITapGestureRecognizer *wenhaoTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(whTapAction)];
    [wenhao addGestureRecognizer:wenhaoTap];
    
    UIButton *explainBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    explainBtn.frame = CGRectMake(ScreenWidth - 40, 26, 40, 32);
    [explainBtn addTarget:self action:@selector(jumpH5VC) forControlEvents:UIControlEventTouchUpInside];
    [_headView addSubview:explainBtn];
    
    
    UILabel *titleLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 32, ScreenWidth, 20)];
    titleLable.text = @"我的收入";
    titleLable.font = [UIFont boldSystemFontOfSize:18];
    titleLable.textAlignment = NSTextAlignmentCenter;
    titleLable.textColor = [UIColor whiteColor];
    [_headView addSubview:titleLable];
    
    UILabel *totdayMoney = [[UILabel alloc] initWithFrame:CGRectMake(0, 70, ScreenWidth, 30)];
    totdayMoney.textAlignment = NSTextAlignmentCenter;
    totdayMoney.font = [UIFont boldSystemFontOfSize:27];
    totdayMoney.textColor = [UIColor whiteColor];
    totdayMoney.text = @"今日总收益(元)";
    [_headView addSubview:totdayMoney];
    
    _moneyLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, ScreenWidth, 30)];
    _moneyLable.textAlignment = NSTextAlignmentCenter;
    _moneyLable.font = [UIFont boldSystemFontOfSize:23];
    _moneyLable.textColor = [UIColor whiteColor];
    _moneyLable.text = @"0.00";
    [_headView addSubview:_moneyLable];
    
    _tableView.tableHeaderView = _headView;
    
    // 底部view
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight - 60, ScreenWidth, 60)];
    bottomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomView];

    UILabel *lineLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 1)];
    lineLable.backgroundColor = [UIColor lightGrayColor];
    lineLable.alpha = 0.2;
    [bottomView addSubview:lineLable];
    
//    // 充值
//    UIButton *chongzhiButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    chongzhiButton.frame = CGRectMake(10, 10, ScreenWidth/2 -15, 40);
//    chongzhiButton.backgroundColor = MainColor;
//    [chongzhiButton setTitle:@"充值" forState:UIControlStateNormal];
//    chongzhiButton.layer.cornerRadius = 5;
//    [chongzhiButton setTintColor:[UIColor whiteColor]];
//    [chongzhiButton addTarget:self action:@selector(chongzhiButtonAction) forControlEvents:UIControlEventTouchUpInside];
//    [bottomView addSubview:chongzhiButton];
    
    // 提现
    UIButton *tixianButton = [UIButton buttonWithType:UIButtonTypeCustom];
    tixianButton.frame = CGRectMake((ScreenWidth-ScreenWidth/2)/2 , 10, ScreenWidth/2, 40);
    tixianButton.backgroundColor = MainColor;
    tixianButton.backgroundColor = MainColor;
    [tixianButton setTitle:@"提现" forState:UIControlStateNormal];
    tixianButton.layer.cornerRadius = 5;
    [tixianButton addTarget:self action:@selector(tixianButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:tixianButton];
    
    
}

-(void)whTapAction
{
    
}


//说明
-(void)jumpH5VC {
    html5MYViewController *html5Vc = [[html5MYViewController alloc] init];
    html5Vc.requestUrl = caiwuExplianurl;
    html5Vc.titleName = @"说明";
    NSLog(@"%@",html5Vc.requestUrl);
    [self.navigationController pushViewController:html5Vc animated:YES];
}
// 充值
-(void)chongzhiButtonAction
{
    if (kNetworkNotReachability) { // 没网
        
        [[MessShowView share] showWithMess:@"当前网络不给力"];
        return;
    }
    NSUserDefaults *userDefaul = [NSUserDefaults standardUserDefaults];
    NSString *iptype = [userDefaul objectForKey:@"iptype"];
    NSString *banben = [userDefaul objectForKey:@"banben"];
    NSString *url = [NSString stringWithFormat:ChongzhiUrl,[AppData share].username,[Utils md5SignStr],iptype,banben];
    html5MYViewController *html5Vc = [[html5MYViewController alloc] init];
    html5Vc.requestUrl = url;
    html5Vc.titleName = @"充值";
    [self.navigationController pushViewController:html5Vc animated:YES];
}
// 提现
-(void)tixianButtonAction
{
    if (kNetworkNotReachability) { // 没网
        
        [[MessShowView share] showWithMess:@"当前网络不给力"];
        return;
    }
    NSUserDefaults *userDefaul = [NSUserDefaults standardUserDefaults];
    NSString *iptype = [userDefaul objectForKey:@"iptype"];
    NSString *banben = [userDefaul objectForKey:@"banben"];
    NSString *url = [NSString stringWithFormat:TixiantUrl,[AppData share].username,[Utils md5SignStr],iptype,banben];
    html5MYViewController *html5Vc = [[html5MYViewController alloc] init];
    html5Vc.requestUrl = url;
    html5Vc.titleName = @"提现";
    [self.navigationController pushViewController:html5Vc animated:YES];
}


-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}



-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0.01;
    }else{
        return 10;
    }
}



-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }else if (section == 1){
        return 3;
    } 
    return 3;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0  ) {
        return 80;
    }else{
        return 50;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *Cell = @"cell";
     CaiWuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Cell];
    
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"CaiWuTableViewCell" owner:self options:nil] firstObject];
    }
 
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.section == 0) {
 
        cell.letfTwoLable.text = cashbalance;
        cell.rightTwoLable.text = invitescore;
        
        return cell;
    }else {
        static NSString *Cell = @"cell";
        OtherTableCell *cell = [tableView dequeueReusableCellWithIdentifier:Cell];
        
        if (!cell) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"OtherTableCell" owner:self options:nil] firstObject];
        }
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.img.frame = CGRectMake(15, 15, 26, 20);
        cell.titleLable.frame = CGRectMake(50, 15, ScreenWidth - 100, 20);
        if (indexPath.row == 0) {
            cell.img.image = [UIImage imageNamed:@"zijinmingxi"];
            cell.titleLable.text = @"资金明细";
        }else if (indexPath.row == 1){
            cell.img.image = [UIImage imageNamed:@"jifenmingxi"];
            cell.titleLable.text = @"积分明细";
        }else if (indexPath.row == 2){
            cell.img.image = [UIImage imageNamed:@"myyinhangka"];
            cell.titleLable.text = @"我的银行卡";
        }
//        else if (indexPath.row == 3){
//            cell.img.image = [UIImage imageNamed:@"shimingrenzheng"];
//            cell.titleLable.text = @"实名认证";
//        }
        
        
        return cell;
    }
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    if (kNetworkNotReachability) { // 没网
//        
//        [[MessShowView share] showWithMess:@"当前网络不给力"];
//        return;
//    }
    
    if (indexPath.section == 1) {
        
        if (indexPath.row == 0) { // 资金明细
            NSUserDefaults *userDefaul = [NSUserDefaults standardUserDefaults];
            NSString *iptype = [userDefaul objectForKey:@"iptype"];
            NSString *banben = [userDefaul objectForKey:@"banben"];
            NSString *url = [NSString stringWithFormat:ZiJinMingXiUrl,[AppData share].username,[Utils md5SignStr],iptype,banben];
            html5MYViewController *html5Vc = [[html5MYViewController alloc] init];
            html5Vc.requestUrl = url;
            html5Vc.showRefresh = @"0";
            html5Vc.titleName = @"资金明细";
            [self.navigationController pushViewController:html5Vc animated:YES];
            
            
        }else if (indexPath.row == 1){ // 积分明细
            NSUserDefaults *userDefaul = [NSUserDefaults standardUserDefaults];
            NSString *iptype = [userDefaul objectForKey:@"iptype"];
            NSString *banben = [userDefaul objectForKey:@"banben"];
            NSString *url = [NSString stringWithFormat:JifenMingXiUrl,[AppData share].username,[Utils md5SignStr],iptype,banben];
            html5MYViewController *html5Vc = [[html5MYViewController alloc] init];
            html5Vc.requestUrl = url;
            html5Vc.showRefresh = @"0";
            html5Vc.titleName = @"积分明细";
            [self.navigationController pushViewController:html5Vc animated:YES];
            
            
        }else if (indexPath.row == 2){ // 我的银行卡
            NSUserDefaults *userDefaul = [NSUserDefaults standardUserDefaults];
            NSString *iptype = [userDefaul objectForKey:@"iptype"];
            NSString *banben = [userDefaul objectForKey:@"banben"];
            NSString *url = [NSString stringWithFormat:MyboundCarUrl,[AppData share].username,[Utils md5SignStr],iptype,banben];
            html5MYViewController *html5Vc = [[html5MYViewController alloc] init];
            html5Vc.requestUrl = url;
            html5Vc.showRefresh = @"1";
            html5Vc.titleName = @"我的银行卡";
            [self.navigationController pushViewController:html5Vc animated:YES];
            
            
        }
    }
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
